/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pb138.srgsxmldb.test;

import cz.muni.fi.pb138.srgsxmldb.fileUtils.FileUtil;
import cz.muni.fi.pb138.srgsxmldb.manager.XManager;
import java.util.ResourceBundle;
import org.basex.core.BaseXException;
import org.basex.core.Context;
import org.basex.core.cmd.Close;
import org.basex.core.cmd.Open;
import org.basex.core.cmd.XQuery;

/**
 * This class demonstrates collection relevant queries.
 * It shows how to find and query specific documents.
 *
 * @author BaseX Team 2005-12, BSD License
 */
public final class QueryCollection {
  /**
   * Runs the example code.
   * @param args (ignored) command-line arguments
   * @throws BaseXException if a database command fails
   */
  public static void main(final String[] args) throws BaseXException {
    /** Database context. */


    System.out.println("=== QueryCollection ===");

    // ------------------------------------------------------------------------
    // Create a collection from all XML documents in the specified directory
    System.out.println("\n* Create a collection.");

    //new CreateDB("Collection", "database/").execute(context);
    
    //new Open("Collection").execute(context);
    
    ResourceBundle rb = ResourceBundle.getBundle("cz.muni.fi.pb138.srgsxmldb.properties/baseXdatabase");
    XManager m = new XManager();
    
    //m.deleteGrammarByFileName("mail.xml");
    
    //m.addGrammarFromXMLFile(new File("kravy.xml"));
    
    Context context = new Context();

    
    new Open("XMLSRGSGrammarCollection").execute(context);
    
    //new Add( "","database/kupp.xml").execute(context);
    //new Delete("kravy.xml").execute(context);

    // ------------------------------------------------------------------------
    // List all documents in the database
    System.out.println("\n* List all documents in the database:");

    // The XQuery base-uri() function returns a file path
    
    String root = "gog";
    String s =(new XQuery(
        "for $doc in collection('"+rb.getString("collectionName") +"')" +
        //"where $doc//@root='gog'" +
        "return concat('',base-uri($doc))"
    ).execute(context));
    
    System.out.println(s);
    
    
    System.out.println("");
    
    String lines[] = s.split("\\s");
    
    for(int i = 0; i<lines.length; i++) {
        
        lines[i] = (FileUtil.extractFileName(lines[i]));        
    }
    
    for(String string: lines) {
        
        System.out.println(string);        
    }

    // ------------------------------------------------------------------------
    // Evaluate a query on a single document
    System.out.println("\n* Evaluate a query on a single document:");

    // If the name of the database is omitted in the collection() function,
    // the currently opened database will be referenced
    String filename = "supp.xml";
    System.out.println(FileUtil.extractFileName(new XQuery(
        "for $doc in collection('"+rb.getString("collectionName") +"')" +
        "let $file-path := base-uri($doc)" +
        "where ends-with($file-path, '" + filename+"')" +
        "return $file-path"
    ).execute(context)));

   /* // ------------------------------------------------------------------------
    // Evaluate a query on a single document
    System.out.println("\n* TEsting query");

    // If the name of the database is omitted in the collection() function,
    // the currently opened database will be referenced
    System.out.println(new XQuery(
        "for $doc in collection('XMLSRGSGrammarCollection')" +
        "let $file-path := base-uri($doc)" +
        "where ends-with($file-path, 'supp.xml')" +
        "return concat($file-path, ' has ', count($doc//*), ' elements')"
    ).execute(context));*/
    
    
    
    // ------------------------------------------------------------------------
    // Drop the database
    //System.out.println("\n* Drop the database.");
    
    

    //new DropDB("Collection").execute(context);

    // ------------------------------------------------------------------------
    // Close the database context
    new Close().execute(context);
    context.close();
  }
}